var server = require('../../utils/server');
var city = require('../../utils/common.js');
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var seat;
var isLoc = false;
var app = getApp()
Page({
  data: {
    "address": "定位中",
    address_id:null,
    banner: [],
    goods: [],
    bannerHeight: Math.ceil(290.0 / 750.0 * getApp().screenWidth),
    homeprolist:[],
    active:0,
    class_list:'',
    secondlist:'',
    fen:false,
    pai:false,
    order_bay:'views',
    shopcate:0,
    page:1,
    gonggao: ['易逛街使用手册—商品分类篇', '易逛街使用手册—商品规格篇', '易逛街使用手册—红包篇', '易逛街使用手册—充值与提现篇', '易逛街使用手册—优惠券篇', '易逛街使用手册—运费篇', '易逛街使用手册—发布商品篇', '易逛街使用手册—商品管理篇', '易逛街使用手册—订单管理篇']
  },
  showCoupon: function (e) {
    wx.navigateTo({
      url: '../getcoupon/getcoupon',
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  showOrder: function (e) {
    wx.navigateTo({
      url: '../order/list/list',
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  showSeller: function (e) {
      // console.log(e)
      wx.showToast({
        title: '正在开发中...',
        icon:'none'
      })
    // wx.navigateTo({
    //     url: '../zushou/index/index',
    // })
  },
  search: function (e) {
    wx.navigateTo({
      url: "../search/index"
    });
  },
  showCarts: function (e) {
    wx.showToast({
      title: '正在开发中...',
      icon: 'none'
    })
    // wx.navigateTo({
    //   url: "../job/job/job"
    // });
  },
  gogongao:function(){
    wx.navigateTo({
      url: '../useinfo/index',
    })
  },
  getfirst:function(){
    let that = this;
    // server.postJSON('/seller/headcategroy',{},(res)=>{
    //     if(res.data.status == 200){
    //         that.setData({
    //           class_list:res.data.list
    //         })
    //     }
    // })
    server.send('/seller/headcategroy', 'POST', { site_id: getApp().globalData.site_id}).then(res => {
          if (res.status == 200) {
              that.setData({
                  class_list: res.list
              })
          }
      }) 

  },
  onChange:function(e){
    // console.log(e)
    let that = this
    let index = e.detail.index
    let first_id= that.data.class_list[index].id
    that.setData({
      shopcate: first_id,
      page: 1,
      homeprolist:[]
    });
    that.getsecond(that.data.shopcate)
    that.getseller(that.data.shopcate, that.data.order_bay)
  },
  getsecond:function(id){
      let that = this
      let data = {
        categroy: id,
        site_id: getApp().globalData.site_id,
        type:''
      };
    // server.postJSON('/seller/secendcategroy',data,(res)=>{
    //     if(res.data.status == 200){
    //       // console.log(res.data.list)
    //         that.setData({
    //           secondlist: res.data.list
    //         })
    //     }
    // })
    server.send('/seller/secendcategroy', 'POST', data).then(res => {
        if (res.status == 200) {
          that.setData({
            secondlist: res.list
          })
        }
    }) 
  },
  clickfen:function(){
      this.setData({
        fen: !this.data.fen,
        pai: false
      })
  },
  clickpai:function(){
    this.setData({
      fen: false,
      pai: !this.data.pai
    })
  },
  clickPaiList:function(e){
      let that = this
      let order_bay = e.currentTarget.dataset.type
      // console.log(order_bay)
      that.setData({
        pai: false,
        order_bay: order_bay,
        page: 1,
        homeprolist: []
      })
    that.getseller(that.data.shopcate, that.data.order_bay)
  },
  clickFenList:function(e){
      let that = this
      let id = e.currentTarget.dataset.id
      // console.log(id)
      that.setData({
        shopcate:id,
        fen: false,
        page: 1,
        homeprolist: []
      })
    that.getseller(that.data.shopcate, that.data.order_bay)
  },
  getseller:function(id,type){
      let that = this
      let data = {
        shopcate:id,
        order_bay:type,
        desc:'desc',
        page:that.data.page,
        perNumber:12,
        site_id: getApp().globalData.site_id,
        type:''
      };
    // server.postJSON('/seller/allseller',data,(res)=>{
    //     // console.log(res)
    //     if(res.data.status == 200){
    //         // console.log(res.data.list)
    //       if (that.data.homeprolist.length == 0) {
    //         var categorys = res.data.list;
    //       }
    //       else {
    //         var categorys = that.data.homeprolist.concat(res.data.list)
    //       }
    //         that.setData({
    //           homeprolist: categorys
    //         })
    //     }
    // })
      server.send('/seller/allseller', 'POST', data).then(res => {
          if (res.status == 200) {
              if (that.data.homeprolist.length == 0) {
                  var categorys = res.list;
              }
              else {
                  var categorys = that.data.homeprolist.concat(res.list)
              }
              that.setData({
                  homeprolist: categorys
              })
          }
      }) 
  },
  onReachBottom: function () {
    let pages = this.data.page + 1
    this.setData({
      page: pages
    })
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    this.getseller(this.data.shopcate, this.data.order_bay)
  },
  onLoad: function (options) {
    // this.getseller(0,'comment_v')
    this.getInviteCode(options);
    getApp().location(); //当前位置信息 => 郑州市|中原区
    this.getfirst()
    let _this = this;
    var app = getApp();
    // _this.getHomeBanner();
  },
  getInviteCode: function (options) {
    if (options.uid != undefined) {
      wx.showToast({
        title: '来自用户:' + options.uid + '的分享',
        icon: 'success',
        duration: 2000
      })
    }
  },
  loadMainGoods: function () {
    var that = this;
    var query = new AV.Query('Goods');
    query.equalTo('isHot', true);
    query.find().then(function (goodsObjects) {
      that.setData({
        goods: goodsObjects
      });
    });
  },
  onShow: function (options) {
    this.setData({
      homeprolist: [],
      page:1
    });
    this.getHomeBanner();
    this.getseller(0, 'views')
    this.getsecond(0)
    var app = getApp();
    var self = this;
    if (isLoc) {
      var address = getApp().globalData.city;
      self.setData({ address: address });
      return;
    }
    var map = new QQMapWX({
      key: 'LAWBZ-2CHCD-MCK4X-PSTUA-NJZJJ-IHFQ2' // 必填
    //   key:'CVHBZ-I2QAX-7CL4N-TJQZ6-UMO4AZ-WDB5I'
        // key: 'UPNBZ-PLR3W-6GXRQ-OO2VH-H3UUZ-A6B4X'
    });
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        app.globalData.lat = latitude;
        app.globalData.lng = longitude;
        // 实例划API核心类
        // console.log(latitude)
        ////address: res.result.address_component.city
        // 调用接口
        
        map.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success: function (res) {
            
            let city = res.result.ad_info.city+res.result.ad_info.district
            if (res.result.ad_info.city != undefined) {
              wx.showModal({
                title: '提示',
                content: '默认地区为周口市淮阳县，是否修改为:' + city,
                success: function(ress){
                  // console.log(ress)
                  if (ress.confirm) {
                      self.setData({
                        address: res.result.ad_info.district
                      });
                      getApp().globalData.city = res.result.ad_info.district;
                      getApp().globalData.site_id = res.result.ad_info.adcode;
                      self.getHomeBanner();
                      self.getsecond(0)
                      self.setData({
                          homeprolist:[]
                      });
                    self.getseller(0, 'views')
                  } else if (ress.cancel) {
                      self.setData({
                        address: getApp().globalData.city
                      });
                  }
                }
              })
              isLoc = true;
              // self.loadBanner();
            }
          },
          fail: function (res) {
          },
          complete: function (res) {
          }
        });
      }
    })
  },
  showDetail: function (e) {
    var goodsId = e.currentTarget.dataset.goodsId;
    wx.navigateTo({
      url: "../goods/detail/detail?objectId=" + goodsId
    });
  },
  showCategories: function () {
    wx.switchTab({
      url: "../category/category"
    });
  },
  showGroupList: function () {
    wx.navigateTo({
      url: "../redpacket/redpacket"
    });
  },
  onShareAppMessage: function () {
    return {
      title: '易逛街',
      desc: '一个由易逛街开发的电商系统',
      path: '/pages/index/index?uid=4719784'
    }
  },
  select: function () {
    wx.navigateTo({
      url: '../switchcity/switchcity',
      success: function (res) {
        // success
      },
      fail: function (res) {
        // fail
      },
      complete: function (res) {
        // complete
      }
    })
  },
  getHomeBanner(){
      
      
    let _params={
      site_id: getApp().globalData.site_id,
      place: 70
    }
    let _this = this;
    wx.getLocation({
      success: function(res) {

      },
    })
    // server.postJSON('/seller/imgs',_params,(res)=>{
    //   if(res.data.status==200){
    //     _this.setData({
    //       banner: res.data.list
    //     })
    //   }
    // })
      server.send('/seller/imgs','POST', _params).then(res=>{
          if (res.status == 200) {
            this.setData({
                banner: res.list
            })
        }
      })
  },
  // gethomeprolist(){
  //   let _params={
  //     site_id:'411626',//定位地址 id  暂定为淮阳
  //     // site_id: this.data.address_id,
  //   }
  //   let _this = this;
  //   server.postJSON('/site/fenlei', _params,(res)=>{
  //     if(res.data.status==200){
  //       this.setData({
  //         homeprolist:res.data.list
  //       })
  //     }
  //   })
  // },
  returnTop(){
    //返回顶部
    wx.pageScrollTo({
      scrollTop: 0,
    })
  },
  tostore:function(e){
    //跳转进入店铺
    let store_id = e.currentTarget.dataset.goodsId;
    // console.log(store_id);
    // return 
    wx.navigateTo({
      // url: '../seller/index?objectId='+store_id,
      // url: '../seller/index?seller_id=780',
      url: '../storedtl/storedtl?objectId=' + store_id,
      // url: '../storedtl/storedtl?seller_id=780',
    })
    // window.location.href ="https://app.yiguangjie.com/dianpu_index/dist_test/index.html?seller_id=780";
  }
})